package org.pb.order.service.impl;

import org.pb.basic.service.impl.BaseServiceImpl;
import org.pb.order.domain.OrderAddress;
import org.pb.order.domain.ProductOrder;
import org.pb.order.mapper.OrderAddressMapper;
import org.pb.order.mapper.ProductOrderDetailMapper;
import org.pb.order.mapper.ProductOrderMapper;
import org.pb.order.query.ProductOrderQuery;
import org.pb.order.service.IProductOrderService;
import org.pb.pay.domain.PayBill;
import org.pb.pay.mapper.PayBillMapper;
import org.pb.pay.service.IPayBillService;
import org.pb.user.domain.LoginInfo;
import org.pb.user.domain.User;
import org.pb.user.domain.UserAddress;
import org.pb.user.mapper.UserAddressMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class ProductOrderServiceImpl extends BaseServiceImpl<ProductOrder, ProductOrderQuery> implements IProductOrderService {

    @Autowired
    private ProductOrderMapper productOrderMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private OrderAddressMapper orderAddressMapper;

    @Autowired
    private ProductOrderDetailMapper productOrderDetailMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IPayBillService payBillService;
    @Override
    public String submit(Map<String, Object> params, LoginInfo loginInfo) {
//        Long addressId = Long.parseLong(params.get("address_id").toString());
//        Long productId = Long.parseLong(params.get("product_id").toString());
//        Long pay_method = Long.parseLong(params.get("pay_method").toString());
//        Product product = productMapper.loadById(productId);
//        UserAddress userAddress = userAddressMapper.loadById(addressId);
//        System.out.println(userAddress);
//        //1创建订单
//        //1.1 创建订单
//        ProductOrder productOrder = getProductOrder(params,userAddress,product,user);
//        productOrderMapper.save(productOrder);
//        //1.2 保存关联表 orderAdress productOrderDetail
//
//        System.out.println(productOrder.getId());
//        OrderAddress orderAddress = userAddress2orderAddress(userAddress);
//        orderAddress.setOrder_id(productOrder.getId());
//        orderAddress.setOrderSn(productOrder.getOrderSn());
//        orderAddressMapper.save(orderAddress);
//
//        ProductOrderDetail productOrderDetail = product2productOrderDetail(product);
//        productOrderDetail.setOrder_id(productOrder.getId());
//        productOrderDetailMapper.save(productOrderDetail);
//
//        // 2 创建支付单
//        PayBill payBill = order2PayBill(pay_method, productOrder, user);
//        payBillMapper.save(payBill);
//
//        // 3 订单倒计时取消处理
//        QuartzJobInfo info = new QuartzJobInfo();
//        info.setType(JobTypeConsts.WAIT_PRODUCT_ORDER_PAY_CANCEL_JOB);
//        String jobName = "ProductOrderCancelJob"+productOrder.getId();
//        Map<String, Object> infoParams = new HashMap<>();
//        infoParams.put("orderId",productOrder.getId());
//        Date fireDate = productOrder.getLastPayTime();
//        info.setJobName(jobName);
//        info.setParams(infoParams);
//        info.setFireDate(fireDate);
//        quartzService.addJob(info);
//        // 4 跳转到支付页面,通过调用支付接口，要把这个数据返回前台，前台处理后跳转支付宝支付页面
//        String payData = payBillService.pay(payBill);
        return null;
    }

    @Override
    public void cancelOrder(Long orderId) {
        ProductOrder productOrder = productOrderMapper.loadById(orderId);
        if(productOrder.getState()!=-1){
            productOrder.setState(-1);
            productOrderMapper.update(productOrder);
        }

        String paySn = productOrder.getPaySn();
        PayBill payBill = payBillMapper.loadByUnionPaySn(paySn);
        if (payBill.getState()!=-1){
            payBill.setState(-1);
            payBillMapper.update(payBill);
        }

    }

    private PayBill order2PayBill(Long pay_method, ProductOrder productOrder,User user) {
        PayBill payBill = new PayBill();
        payBill.setDigest(productOrder.getDigest());
        payBill.setMoney(productOrder.getPrice());
        payBill.setUnionPaySn(productOrder.getPaySn());
        payBill.setState(0);
        payBill.setLastPayTime(productOrder.getLastPayTime());
        payBill.setPayChannel(pay_method);
//        payBill.setBusinessType(PayConstants.BUSINESSTYPE_PRODUCT);
        payBill.setBusinessKey(productOrder.getId());
        payBill.setCreateTime(new Date());
        payBill.setUser_id(productOrder.getUser_id());
        payBill.setNickName(user.getUsername());
        payBill.setShop_id(productOrder.getShop_id());
        return payBill;
    }


    private OrderAddress userAddress2orderAddress(UserAddress userAddress) {
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(userAddress,orderAddress);
        orderAddress.setContacts(userAddress.getContacts());
        return orderAddress;
    }

}
